package com.roamer.rest.common;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;

import java.io.IOException;

/**
 * 用户信息 HTTP调用拦截器
 *
 * @author roamer
 * @version V1.0
 * @date 2019-07-22 15:15
 */
@Slf4j
public class UserContextInterceptor implements ClientHttpRequestInterceptor {

    @Override
    public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution)
            throws IOException {
        HttpHeaders headers = request.getHeaders();
        headers.add(Constants.CORRELATION_ID, UserContextHolder.getUserContext().getCorrelationId());
        headers.add(Constants.AUTH_TOKEN, UserContextHolder.getUserContext().getAuthToken());
        return execution.execute(request, body);
    }
}
